<!DOCTYPE html>
<html lang=zh>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>solidity基础部分 | Hexo</title>
  <meta name="description" content="solidity学习solidity相关Remix solidity Docs 一、合约初始设置1.solidity版本的指定 指定特定0.8.7的solidity版本  1pragma solidity 0.8.7;   指定高于0.8.7版本的编译器  1pragma solidity ^0.8.7;   指定编译器的版本介于0.8.7到0.9.0  1pragma solidity &gt;">
<meta property="og:type" content="article">
<meta property="og:title" content="solidity基础部分">
<meta property="og:url" content="https://imynniu.gitee.io/notes/2022/11/27/solidity%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="wStar">
<meta property="og:description" content="solidity学习solidity相关Remix solidity Docs 一、合约初始设置1.solidity版本的指定 指定特定0.8.7的solidity版本  1pragma solidity 0.8.7;   指定高于0.8.7版本的编译器  1pragma solidity ^0.8.7;   指定编译器的版本介于0.8.7到0.9.0  1pragma solidity &gt;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imynniu.gitee.io/notes/....%5Cimages%5Csolidity%5CuTools_1668394833158.png">
<meta property="og:image" content="d:\github\Blog\images\solidity\uTools_1668396478849.png">
<meta property="og:image" content="d:\github\Blog\images\solidity\uTools_1668484321400.png">
<meta property="article:published_time" content="2022-11-26T16:00:00.000Z">
<meta property="article:modified_time" content="2023-03-27T10:49:27.154Z">
<meta property="article:author" content="Imynniu">
<meta property="article:tag" content="solidity">
<meta property="article:tag" content="fullStackWeb3">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://imynniu.gitee.io/notes/....%5Cimages%5Csolidity%5CuTools_1668394833158.png">
  <!-- Canonical links -->
  <link rel="canonical" href="https://imynniu.gitee.io/notes/2022/11/27/solidity%E5%9F%BA%E7%A1%80/index.html">
  
    <link rel="alternate" href="/atom.xml" title="wStar" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/notes/css/style.css">

  
  
  
    <link href="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.3.5/dist/jquery.fancybox.min.css" rel="stylesheet">
  
  
<meta name="generator" content="Hexo 6.3.0"></head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://gitee.com/imynniu" target="_blank">
          <img class="img-circle img-rotate" src="/notes/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">Imynniu</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Web3 enthusiast &amp; Student</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> DaLian, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/notes/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/notes/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/notes/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/notes/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/notes/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">项目</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/notes/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/imynniu" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p><br><a herf="https://imynniu.gitee.io/">点此返回主页<a/>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Other/">Other</a><span class="category-list-count">2</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Other/Git/">Git</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Other/MarkDown/">MarkDown</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Web3/">Web3</a><span class="category-list-count">3</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Web3/ether-js/">ether.js</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Web3/solidity/">solidity</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/solidity/">solidity</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E5%8C%BA%E5%9D%97%E9%93%BE/">区块链</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E5%8C%BA%E5%9D%97%E9%93%BE/MEV/">MEV</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E8%8B%B1%E8%AF%AD/">英语</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E8%8B%B1%E8%AF%AD/%E8%8B%B1%E8%AF%AD%E9%9F%B3%E6%A0%87/">英语音标</a><span class="category-list-count">1</span></li></ul></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/Git/" rel="tag">Git</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/MarkDown/" rel="tag">MarkDown</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/PBS/" rel="tag">PBS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/ethereum/" rel="tag">ethereum</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/fullStackWeb3/" rel="tag">fullStackWeb3</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/javaScript/" rel="tag">javaScript</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/solidity/" rel="tag">solidity</a><span class="tag-list-count">12</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E4%BB%A5%E5%A4%AA%E5%9D%8A/" rel="tag">以太坊</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E5%B7%A5%E5%85%B7/" rel="tag">工具</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E8%8B%B1%E8%AF%AD/" rel="tag">英语</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E8%8B%B1%E8%AF%AD%E9%9F%B3%E6%A0%87/" rel="tag">英语音标</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/notes/tags/Git/" style="font-size: 13px;">Git</a> <a href="/notes/tags/MarkDown/" style="font-size: 13px;">MarkDown</a> <a href="/notes/tags/PBS/" style="font-size: 13px;">PBS</a> <a href="/notes/tags/ethereum/" style="font-size: 13px;">ethereum</a> <a href="/notes/tags/fullStackWeb3/" style="font-size: 13.67px;">fullStackWeb3</a> <a href="/notes/tags/javaScript/" style="font-size: 13px;">javaScript</a> <a href="/notes/tags/solidity/" style="font-size: 14px;">solidity</a> <a href="/notes/tags/%E4%BB%A5%E5%A4%AA%E5%9D%8A/" style="font-size: 13px;">以太坊</a> <a href="/notes/tags/%E5%B7%A5%E5%85%B7/" style="font-size: 13.33px;">工具</a> <a href="/notes/tags/%E8%8B%B1%E8%AF%AD/" style="font-size: 13px;">英语</a> <a href="/notes/tags/%E8%8B%B1%E8%AF%AD%E9%9F%B3%E6%A0%87/" style="font-size: 13px;">英语音标</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/notes/archives/2023/04/">四月 2023</a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/notes/archives/2023/03/">三月 2023</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/notes/archives/2022/11/">十一月 2022</a><span class="archive-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled ">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson4/" class="title">solidity中的特殊函数</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson5-1/" class="title">js的异步编程、编译智能合约、Ether.js部署合约与发送交易</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson5-2/" class="title">js的异步编程、编译智能合约、Ether.js部署合约与发送交易</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson5/" class="title">wsl环境配置</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/01/04-01-Lesson3/" class="title">可组合性,合约之间的交互,继承和重写</a>
              </p>
              <p class="item-date">
                <time datetime="2023-03-31T16:00:00.000Z" itemprop="datePublished">2023-04-01</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-solidity基础" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      solidity基础部分
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/notes/2022/11/27/solidity%E5%9F%BA%E7%A1%80/" class="article-date">
	  <time datetime="2022-11-26T16:00:00.000Z" itemprop="datePublished">2022-11-27</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/notes/categories/Web3/">Web3</a>►<a class="article-category-link" href="/notes/categories/Web3/solidity/">solidity</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link-link" href="/notes/tags/fullStackWeb3/" rel="tag">fullStackWeb3</a>, <a class="article-tag-link-link" href="/notes/tags/solidity/" rel="tag">solidity</a>
  </span>


        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/notes/2022/11/27/solidity%E5%9F%BA%E7%A1%80/#comments" class="article-comment-link">评论</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 3.6k(字)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 15(分)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <h1 id="solidity学习"><a href="#solidity学习" class="headerlink" title="solidity学习"></a>solidity学习</h1><h3 id="solidity相关"><a href="#solidity相关" class="headerlink" title="solidity相关"></a>solidity相关</h3><p><a target="_blank" rel="noopener" href="https://remix.ethereum.org/">Remix</a></p>
<p><a target="_blank" rel="noopener" href="https://docs.soliditylang.org/en/v0.8.17/installing-solidity.html">solidity Docs</a></p>
<h3 id="一、合约初始设置"><a href="#一、合约初始设置" class="headerlink" title="一、合约初始设置"></a>一、合约初始设置</h3><h4 id="1-solidity版本的指定"><a href="#1-solidity版本的指定" class="headerlink" title="1.solidity版本的指定"></a>1.solidity版本的指定</h4><ul>
<li>指定特定0.8.7的solidity版本</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pragma solidity 0.8.7;</span><br></pre></td></tr></table></figure>

<ul>
<li>指定高于0.8.7版本的编译器</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pragma solidity ^0.8.7;</span><br></pre></td></tr></table></figure>

<ul>
<li>指定编译器的版本介于0.8.7到0.9.0</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pragma solidity &gt;=0.8.7 &lt;0.9.0;</span><br></pre></td></tr></table></figure>

<h4 id="2-注释"><a href="#2-注释" class="headerlink" title="2. 注释"></a>2. 注释</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">//使用双斜杠进行注释</span><br></pre></td></tr></table></figure>

<h4 id="3-开源协议"><a href="#3-开源协议" class="headerlink" title="3. 开源协议"></a>3. 开源协议</h4><p>开源协议放到首行，是一个可选项，没有的话编译器会警告.</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br></pre></td></tr></table></figure>

<h4 id="4-合约声明"><a href="#4-合约声明" class="headerlink" title="4. 合约声明"></a>4. 合约声明</h4><p>使用关键词contract，声明类似于类的方式。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">contract SimpleStorage&#123;</span><br><span class="line"></span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h3 id="二、基础数据类型"><a href="#二、基础数据类型" class="headerlink" title="二、基础数据类型"></a>二、基础数据类型</h3><p>包含Boolean，uint，int，address，bytes等，具体需要查看文档。</p>
<h4 id="1-boolean"><a href="#1-boolean" class="headerlink" title="1. boolean"></a>1. boolean</h4><p>true or flase</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//boolean type</span><br><span class="line">bool hasFavoriteNumber = true;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="2-unit"><a href="#2-unit" class="headerlink" title="2. unit"></a>2. unit</h4><ul>
<li>无符号整数，可以指定多少位分配给这个数据。默认是256位</li>
<li>不设置初始值默认为0</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint8 FavoriteNumber = 123;</span><br><span class="line">//不设置初始值默认为0</span><br><span class="line">unit Number;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="3-int"><a href="#3-int" class="headerlink" title="3. int"></a>3. int</h4><p>有符号整数，也可以分配位数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//int type</span><br><span class="line">int8 FavoriteNumber = -5;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="4-string"><a href="#4-string" class="headerlink" title="4. string"></a>4. string</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//string type</span><br><span class="line">string FavoriteNumber = &quot;wstar&quot;;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="5-address"><a href="#5-address" class="headerlink" title="5. address"></a>5. address</h4><p>可以是以太坊的账户地址。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//address type</span><br><span class="line">address FavoriteNumber = 0xAC8135b2cCd8E81A5e9dB7edafB23B6C41c46907;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="6-Byte"><a href="#6-Byte" class="headerlink" title="6. Byte"></a>6. Byte</h4><ul>
<li>一个byte是一个字节，包含8位。solidity中最多申请256位，也就是32字节，所以byte可以自定义字节长度但是不能超过32字节。</li>
<li>不填写字节数的话表示可以有任意大小</li>
<li>string类型可以转换位byte</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//byte type</span><br><span class="line">bytes32 FavoriteNumber = &quot;cats&quot;;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h3 id="三、函数"><a href="#三、函数" class="headerlink" title="三、函数"></a>三、函数</h3><h4 id="1-函数结构"><a href="#1-函数结构" class="headerlink" title="1. 函数结构"></a>1. 函数结构</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint favoriteNumber;</span><br><span class="line"></span><br><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="2-可见性"><a href="#2-可见性" class="headerlink" title="2.可见性"></a>2.可见性</h4><p>官方文档 :  <a target="_blank" rel="noopener" href="https://docs.soliditylang.org/en/latest/contracts.html#visibility-and-getters">Visibility</a></p>
<p>函数与变量可以有其中之一的可见性：Public，private，external，internal。</p>
<p>可见性在状态变量的类型之后给出，在函数的参数列表和返回值之间给出。</p>
<h5 id="a-状态变量的可见性（public、internal、private）"><a href="#a-状态变量的可见性（public、internal、private）" class="headerlink" title="a. 状态变量的可见性（public、internal、private）"></a>a. 状态变量的可见性（public、internal、private）</h5><p>private和external只是阻止其他合约的读取和修改，对于整个区块链是可见的。</p>
<h6 id="public"><a href="#public" class="headerlink" title="public"></a>public</h6><ul>
<li>public修饰的变量会自动生成getter函数</li>
<li>允许其他合约访问</li>
<li>外部访问的话会通过getter函数，内部访问直接通过存储</li>
<li>没有setter函数，其他合约不能直接修改</li>
</ul>
<h6 id="internal"><a href="#internal" class="headerlink" title="internal"></a>internal</h6><ul>
<li>没有自动生成getter函数</li>
<li>不能外部访问，只能通过自身合约或者继承它的合约访问</li>
</ul>
<h6 id="private"><a href="#private" class="headerlink" title="private"></a>private</h6><ul>
<li>没有getter函数</li>
<li>只能合约内部访问，继承它的合约也不能访问</li>
</ul>
<h5 id="b-函数的可见性（public、internal、private、external）"><a href="#b-函数的可见性（public、internal、private、external）" class="headerlink" title="b. 函数的可见性（public、internal、private、external）"></a>b. 函数的可见性（public、internal、private、external）</h5><p>solidity知道两种函数调用：外部调用会产生EVM消息，内部调用则不会产生EVM消息。</p>
<h6 id="external"><a href="#external" class="headerlink" title="external"></a>external</h6><ul>
<li>external 函数是合约的接口</li>
<li>其他合约可以通过交易调用</li>
<li>external函数f，在内部调用的时候不能直接调用f(),需要使用this.f()调用</li>
</ul>
<h6 id="public-1"><a href="#public-1" class="headerlink" title="public"></a>public</h6><ul>
<li>public 函数也是合约接口</li>
<li>既可以内部调用也可以通过消息调用</li>
</ul>
<h6 id="internal-1"><a href="#internal-1" class="headerlink" title="internal"></a>internal</h6><ul>
<li>只能在当前合约或者继承它的合约内调用，不能外部访问</li>
<li>不会通过合约的ABI暴露</li>
<li>可以获取内部类型的参数</li>
</ul>
<h6 id="private-1"><a href="#private-1" class="headerlink" title="private"></a>private</h6><ul>
<li>和internal函数类似，但是在继承它的合约内也不能访问</li>
</ul>
<h3 id="3-scope"><a href="#3-scope" class="headerlink" title="3. scope"></a>3. scope</h3><p>类似于其他的编程语言Java 、python、js等。</p>
<h4 id="4-状态可变性（view-amp-pure-function）"><a href="#4-状态可变性（view-amp-pure-function）" class="headerlink" title="4. 状态可变性（view&amp;pure function）"></a>4. 状态可变性（view&amp;pure function）</h4><p>view和pure 修饰的函数代表了该函数不需要花费Gas去运行,只有修改区块链的状态产生一笔交易才会花费Gas。自己调用view或者pure函数并不会产生交易。直接调用view或者pure函数不会花费Gas，但是在其他修改区块链状态的函数里调用view或pure函数是需要计算Gas的。</p>
<h4 id="view-函数"><a href="#view-函数" class="headerlink" title="view 函数"></a>view 函数</h4><ul>
<li>不修改区块链的状态</li>
<li>getter函数会默认设置为view函数</li>
<li>只是从区块链中读取数据</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint256 public  favoriteNumber;</span><br><span class="line"></span><br><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">//view</span><br><span class="line">function retrieve() public view  returns(uint256)&#123;</span><br><span class="line">	return favoriteNumber;</span><br><span class="line">&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>



<h4 id="pure-函数"><a href="#pure-函数" class="headerlink" title="pure 函数"></a>pure 函数</h4><ul>
<li><p>不会修改区块链的状态</p>
</li>
<li><p>也不允许从区块链中读取数据</p>
</li>
<li><p>只是单纯的计算</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint256 public  favoriteNumber;</span><br><span class="line"></span><br><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">//view</span><br><span class="line">function retrieve() public view  returns(uint256)&#123;</span><br><span class="line">	return favoriteNumber;</span><br><span class="line">&#125;</span><br><span class="line">//pure</span><br><span class="line">function add() public pure returns(uint256)&#123;</span><br><span class="line">return (20+3);</span><br><span class="line">&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h3 id="四、Array-and-Struct"><a href="#四、Array-and-Struct" class="headerlink" title="四、Array and Struct"></a>四、Array and Struct</h3><h4 id="struct"><a href="#struct" class="headerlink" title="struct"></a>struct</h4><ul>
<li>一个结构体相当于生成一个新的变量类型</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint256 public  favoriteNumber;</span><br><span class="line">//创建一个结构体变量</span><br><span class="line">People public person =People(&#123;favoriteNumber:23,name:&quot;wstar&quot;&#125;);</span><br><span class="line"></span><br><span class="line">//结构体的声明</span><br><span class="line">struct People&#123;</span><br><span class="line">uint256 favoriteNumber;</span><br><span class="line">string name;</span><br><span class="line">&#125;</span><br><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">//view</span><br><span class="line">function retrieve() public view  returns(uint256)&#123;</span><br><span class="line">	return favoriteNumber;</span><br><span class="line">&#125;</span><br><span class="line">//pure</span><br><span class="line">function add() public pure returns(uint256)&#123;</span><br><span class="line">return (20+3);</span><br><span class="line">&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="array"><a href="#array" class="headerlink" title="array"></a>array</h4><h5 id="数组的声明方式"><a href="#数组的声明方式" class="headerlink" title="数组的声明方式"></a>数组的声明方式</h5><blockquote>
<p>变量类型[] 可见性 数组名</p>
<p>People[]  public persons;</p>
<p>uint256[] public favoriteNumbers;</p>
</blockquote>
<h5 id="数组的大小"><a href="#数组的大小" class="headerlink" title="数组的大小"></a>数组的大小</h5><ul>
<li><p>未指定个数的话表示动态的，可以任意多个</p>
</li>
<li><p>给定个数表示最多这么多个</p>
</li>
</ul>
<blockquote>
<p>People[5] public person;</p>
</blockquote>
<h5 id="数组的push-方法"><a href="#数组的push-方法" class="headerlink" title="数组的push 方法"></a>数组的push 方法</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint256   favoriteNumber;</span><br><span class="line">//创建一个结构体变量</span><br><span class="line">//People public person =People(&#123;favoriteNumber:23,name:&quot;wstar&quot;&#125;);</span><br><span class="line">People[] public people;</span><br><span class="line">//结构体的声明</span><br><span class="line">struct People&#123;</span><br><span class="line">uint256 favoriteNumber;//index=0</span><br><span class="line">string name;//index = 1</span><br><span class="line">&#125;</span><br><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">//view</span><br><span class="line">function retrieve() public view  returns(uint256)&#123;</span><br><span class="line">	return favoriteNumber;</span><br><span class="line">&#125;</span><br><span class="line">//pure</span><br><span class="line">function add() public pure returns(uint256)&#123;</span><br><span class="line">return (20+3);</span><br><span class="line">&#125;</span><br><span class="line">//数组的push方法</span><br><span class="line">function addPeople(string memory _name,uint256 _favorityNumber) public &#123;</span><br><span class="line">	people.push(People(_favorityNumber,_name));</span><br><span class="line">&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h3 id="五、Memory，storage，Calldata"><a href="#五、Memory，storage，Calldata" class="headerlink" title="五、Memory，storage，Calldata"></a>五、Memory，storage，Calldata</h3><h4 id="EVM-Overview"><a href="#EVM-Overview" class="headerlink" title="EVM Overview"></a>EVM Overview</h4><ul>
<li>EVM存储信息的六个地方</li>
</ul>
<ol>
<li>Stack</li>
<li>Memory</li>
<li>Storage</li>
<li>Calldata</li>
<li>Code</li>
<li>Logs</li>
</ol>
<h4 id="Memory，storage，Calldata"><a href="#Memory，storage，Calldata" class="headerlink" title="Memory，storage，Calldata"></a>Memory，storage，Calldata</h4><ul>
<li>Memory 和Calldata修饰的变量只是暂时存在,此处的_name变量只有在该函数调用的时候存在。</li>
</ul>
<blockquote>
<p>&#x2F;&#x2F;数组的push方法<br>function addPeople(string memory _name,uint256 _favorityNumber) public {<br>    people.push(People(_favorityNumber,_name));<br>}</p>
</blockquote>
<ul>
<li>storage修饰的变量，函数不执行也存在,favoriteNumber是默认的storage类型</li>
</ul>
<blockquote>
<p>uint256   favoriteNumber;</p>
</blockquote>
<ul>
<li>Calldata是不可以修改的临时变量</li>
<li>memory是可以修改的临时变量</li>
<li>storage是可以修改的永久变量</li>
<li>array ，struct，mapping需要指定变量的存储类型，其中string属于数组，需要指定存储类型</li>
</ul>
<h3 id="六、Mappings"><a href="#六、Mappings" class="headerlink" title="六、Mappings"></a>六、Mappings</h3><p>使用该类型可以快速索引键值对。</p>
<h4 id="map的声明方式"><a href="#map的声明方式" class="headerlink" title="map的声明方式"></a>map的声明方式</h4><blockquote>
<p>mapping(key type &#x3D;&gt;value type) 可见性 map变量名；</p>
</blockquote>
<h4 id="map的添加方式"><a href="#map的添加方式" class="headerlink" title="map的添加方式"></a>map的添加方式</h4><blockquote>
<p>map变量名[key] &#x3D; value;</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint256   favoriteNumber;</span><br><span class="line">//创建一个结构体变量</span><br><span class="line">//People public person =People(&#123;favoriteNumber:23,name:&quot;wstar&quot;&#125;);</span><br><span class="line"></span><br><span class="line">//声明一个mapping</span><br><span class="line">mapping(string =&gt; uint256) public nameToNumber; </span><br><span class="line"></span><br><span class="line">//声明一个数组</span><br><span class="line">People[] public people;</span><br><span class="line">//结构体的声明</span><br><span class="line">struct People&#123;</span><br><span class="line">uint256 favoriteNumber;//index=0</span><br><span class="line">string name;//index = 1</span><br><span class="line">&#125;</span><br><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">//view</span><br><span class="line">function retrieve() public view  returns(uint256)&#123;</span><br><span class="line">	return favoriteNumber;</span><br><span class="line">&#125;</span><br><span class="line">//pure</span><br><span class="line">function add() public pure returns(uint256)&#123;</span><br><span class="line">return (20+3);</span><br><span class="line">&#125;</span><br><span class="line">//数组的push方法,mapping的添加方法</span><br><span class="line">function addPeople(string memory _name,uint256 _favorityNumber) public &#123;</span><br><span class="line">	people.push(People(_favorityNumber,_name));</span><br><span class="line">	nameToNumber[_name] = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h3 id="七、Deploy-contract"><a href="#七、Deploy-contract" class="headerlink" title="七、Deploy contract"></a>七、Deploy contract</h3><ol>
<li>选择injected Provider-MetaMask</li>
<li>连接Meta mask</li>
<li>Deploy</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">//uint type</span><br><span class="line">uint256   favoriteNumber;</span><br><span class="line">//创建一个结构体变量</span><br><span class="line">//People public person =People(&#123;favoriteNumber:23,name:&quot;wstar&quot;&#125;);</span><br><span class="line"></span><br><span class="line">//声明一个mapping</span><br><span class="line">mapping(string =&gt; uint256) public nameToNumber; </span><br><span class="line"></span><br><span class="line">//声明一个数组</span><br><span class="line">People[] public people;</span><br><span class="line">//结构体的声明</span><br><span class="line">struct People&#123;</span><br><span class="line">uint256 favoriteNumber;//index=0</span><br><span class="line">string name;//index = 1</span><br><span class="line">&#125;</span><br><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">//view</span><br><span class="line">function retrieve() public view  returns(uint256)&#123;</span><br><span class="line">	return favoriteNumber;</span><br><span class="line">&#125;</span><br><span class="line">//pure</span><br><span class="line">function add() public pure returns(uint256)&#123;</span><br><span class="line">return (20+3);</span><br><span class="line">&#125;</span><br><span class="line">//数组的push方法,mapping的添加方法</span><br><span class="line">function addPeople(string memory _name,uint256 _favorityNumber) public &#123;</span><br><span class="line">	people.push(People(_favorityNumber,_name));</span><br><span class="line">	nameToNumber[_name] = _favorityNumber;</span><br><span class="line">&#125;</span><br><span class="line">&#125; </span><br><span class="line">//0x9d83e140330758a8fFD07F8Bd73e86ebcA8a5692</span><br></pre></td></tr></table></figure>

<h3 id="八、导入contract"><a href="#八、导入contract" class="headerlink" title="八、导入contract"></a>八、导入contract</h3><h4 id="1-可组构性（Composibility）"><a href="#1-可组构性（Composibility）" class="headerlink" title="1. 可组构性（Composibility）"></a>1. 可组构性（Composibility）</h4><p>一个智能合约可以为我们部署另一个智能合约，然后来自其他地方的合约进行交互。</p>
<h4 id="2-关键字-new"><a href="#2-关键字-new" class="headerlink" title="2. 关键字 new"></a>2. 关键字 new</h4><ul>
<li>new关键字是指令当前的只能&#x3D;&#x3D;智能合约为我们部署一个智能合约</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity ^0.8.0;</span><br><span class="line"></span><br><span class="line">//SimpleStorage</span><br><span class="line">contract SimpleStorage&#123;</span><br><span class="line">...</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">//StorageFactory</span><br><span class="line">contract StorageFactory&#123;</span><br><span class="line">    SimpleStorage public simpleStorage;</span><br><span class="line">    function createSimpleStorageContract() public &#123;</span><br><span class="line">        simpleStorage = new SimpleStorage();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-使用import关键字"><a href="#3-使用import关键字" class="headerlink" title="3. 使用import关键字"></a>3. 使用import关键字</h4><ul>
<li>使用import关键字将其他合约导入到本合约当中，等同于拷贝过来，易语书写和修改</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity ^0.8.0;</span><br><span class="line"></span><br><span class="line">import &quot;./SimpleStorage.sol&quot;;</span><br><span class="line"></span><br><span class="line">contract StorageFactory&#123;</span><br><span class="line">    SimpleStorage public simpleStorage;</span><br><span class="line">    function createSimpleStorageContract() public &#123;</span><br><span class="line">        simpleStorage = new SimpleStorage();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>导入的contract 的solidity版本要和本地的智能合约版本保持一致，要么都是0.8，要么是0.7…</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity ^0.8.0;</span><br><span class="line"></span><br><span class="line">import &quot;./SimpleStorage.sol&quot;;</span><br><span class="line"></span><br><span class="line">contract StorageFactory&#123;</span><br><span class="line">    //使用数组存储智能合约</span><br><span class="line">    SimpleStorage[] public simpleStorageArray;</span><br><span class="line">    //部署SimpleStorage智能合约</span><br><span class="line">    function createSimpleStorageContract() public &#123;</span><br><span class="line">        SimpleStorage simpleStorage = new SimpleStorage();</span><br><span class="line">        simpleStorageArray.push(simpleStorage);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="九、与其他智能合约进行交互"><a href="#九、与其他智能合约进行交互" class="headerlink" title="九、与其他智能合约进行交互"></a>九、与其他智能合约进行交互</h3><h4 id="1-交互的条件"><a href="#1-交互的条件" class="headerlink" title="1.交互的条件"></a>1.交互的条件</h4><ul>
<li>contract address</li>
<li>ABI —Application Binary Interface</li>
</ul>
<h5 id="获取ABI的两种方式"><a href="#获取ABI的两种方式" class="headerlink" title="获取ABI的两种方式"></a>获取ABI的两种方式</h5><h6 id="（1）通过保存的contract-address"><a href="#（1）通过保存的contract-address" class="headerlink" title="（1）通过保存的contract address"></a>（1）通过保存的contract address</h6><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity ^0.8.0;</span><br><span class="line"></span><br><span class="line">import &quot;./SimpleStorage.sol&quot;;</span><br><span class="line"></span><br><span class="line">contract StorageFactory&#123;</span><br><span class="line">    //使用数组存储智能合约</span><br><span class="line">    SimpleStorage[] public simpleStorageArray;</span><br><span class="line">    //部署SimpleStorage智能合约</span><br><span class="line">    function createSimpleStorageContract() public &#123;</span><br><span class="line">        SimpleStorage simpleStorage = new SimpleStorage();</span><br><span class="line">        simpleStorageArray.push(simpleStorage);</span><br><span class="line">    &#125;</span><br><span class="line">    function sfFtore(uint256 _SimpleStorageIndex,uint256 _SimpleStorageNumber) public &#123;</span><br><span class="line">        //address</span><br><span class="line">        //ABI</span><br><span class="line">        //方式一,不使用new关键字，在SimpleStorage（）传入contract address</span><br><span class="line">        SimpleStorage simpleStorage = SimpleStorage(simpleStorageArray[_SimpleStorageIndex]);</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="2-通过存储的simpleStorageArray-数组，数组内存储的就是智能合约"><a href="#2-通过存储的simpleStorageArray-数组，数组内存储的就是智能合约" class="headerlink" title="2.通过存储的simpleStorageArray 数组，数组内存储的就是智能合约"></a>2.通过存储的simpleStorageArray 数组，数组内存储的就是智能合约</h6><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">function sfFtore(uint256 _SimpleStorageIndex,uint256 _SimpleStorageNumber) public &#123;</span><br><span class="line">    //address</span><br><span class="line">    //ABI</span><br><span class="line">    //方式二,直接使用保存的合约数组</span><br><span class="line">    SimpleStorage simpleStorage = simpleStorageArray[_SimpleStorageIndex];</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="2-交互"><a href="#2-交互" class="headerlink" title="2.交互"></a>2.交互</h4><ul>
<li>获取到合约对象，并将其存储在变量中</li>
<li>变量直接就能调用原合约中的函数</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity ^0.8.0;</span><br><span class="line"></span><br><span class="line">import &quot;./SimpleStorage.sol&quot;;</span><br><span class="line"></span><br><span class="line">contract StorageFactory&#123;</span><br><span class="line">    //使用数组存储智能合约</span><br><span class="line">    SimpleStorage[] public simpleStorageArray;</span><br><span class="line">    //部署SimpleStorage智能合约</span><br><span class="line">    function createSimpleStorageContract() public &#123;</span><br><span class="line">        SimpleStorage simpleStorage = new SimpleStorage();</span><br><span class="line">        simpleStorageArray.push(simpleStorage);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    //与simplestorage contract进行交互，存入favoriteNumber</span><br><span class="line">    function sfFtore(uint256 _SimpleStorageIndex,uint256 _SimpleStorageNumber) public &#123;</span><br><span class="line">        //address</span><br><span class="line">        //ABI</span><br><span class="line">        //方式二,不使用new关键字，在SimpleStorage（）传入contract address</span><br><span class="line">        SimpleStorage simpleStorage = simpleStorageArray[_SimpleStorageIndex];</span><br><span class="line">        simpleStorage.store(_SimpleStorageNumber);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    //与simplestorage contract进行交互，读取favoriteNumber</span><br><span class="line">    function sfGet(uint256 _SimpleStorageIndex) public view returns(uint256)&#123;</span><br><span class="line">        SimpleStorage simpleStorage =simpleStorageArray[_SimpleStorageIndex];</span><br><span class="line">        return simpleStorage.retrieve();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="十、继承与重写"><a href="#十、继承与重写" class="headerlink" title="十、继承与重写"></a>十、继承与重写</h3><p>通过继承和重写，可以为我们已经有的合约增加额外的功能特性。</p>
<h4 id="1-继承的步骤"><a href="#1-继承的步骤" class="headerlink" title="1.继承的步骤"></a>1.继承的步骤</h4><ol>
<li>需要import其他的智能合约</li>
<li>本地contract is othercontract</li>
<li>ExtraStorage contract 就会和SimpleStorage contract一样。</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">//导入sol，相当于复制过来</span><br><span class="line">import &quot;./SimpleStorage.sol&quot;;</span><br><span class="line">//使用is关键字表示继承</span><br><span class="line">contract ExtraStorage is SimpleStorage&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-重写"><a href="#2-重写" class="headerlink" title="2. 重写"></a>2. 重写</h4><ul>
<li>需要在原合约中要重写的函数增加Virtual标识</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//定义一个存储函数</span><br><span class="line">function store(uint256 _favorityNumber) public virtual &#123;</span><br><span class="line">	favoriteNumber = _favorityNumber;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>在新合约的重写方法中增加override标识</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line"></span><br><span class="line">import &quot;./SimpleStorage.sol&quot;;</span><br><span class="line"></span><br><span class="line">contract ExtraStorage is SimpleStorage&#123;</span><br><span class="line">    //重写store函数</span><br><span class="line">    function store(uint256 _favorityNumber) public override &#123;</span><br><span class="line">        favoriteNumber = _favorityNumber + 5;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="十一、Sending-ETH-through-a-function-amp-Revert"><a href="#十一、Sending-ETH-through-a-function-amp-Revert" class="headerlink" title="十一、Sending ETH through a function &amp; Revert"></a>十一、Sending ETH through a function &amp; Revert</h3><h4 id="1-交易的中的值"><a href="#1-交易的中的值" class="headerlink" title="1. 交易的中的值"></a>1. 交易的中的值</h4><ul>
<li>Nonce : 该账户的交易计数</li>
<li>Gas Price ：单位gas的价格（wei）</li>
<li>GasLimit：该交易最多花费的gas</li>
<li>To： 发送的地址</li>
<li>Value： 发送的wei的数量</li>
<li>Data：发送到To地址的信息</li>
<li>v，r，s交易签名的组件</li>
</ul>
<h4 id="2-payable"><a href="#2-payable" class="headerlink" title="2. payable"></a>2. payable</h4><ul>
<li>调用函数时，value值是值向合约发送的多少wei</li>
<li>若函数没有标记payable，则函数的调用value必须为0</li>
<li>只有设置了payable标记才能修改value的值</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract FundMe &#123;</span><br><span class="line">    // fund函数，所有人都可以向该合约发送代币</span><br><span class="line">    function fund() public payable&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-mas-value-amp-other-global-keywords"><a href="#3-mas-value-amp-other-global-keywords" class="headerlink" title="3.mas.value &amp; other global keywords"></a>3.mas.value &amp; other global keywords</h4><p>msg.value 是发送者发送的value</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">//SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract FundMe &#123;</span><br><span class="line">    uint256 public value;</span><br><span class="line">    // fund函数，所有人都可以向该合约发送代币</span><br><span class="line">    function fund() public payable&#123;</span><br><span class="line">        //获取到发送的value值</span><br><span class="line">        value =msg.value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-require"><a href="#4-require" class="headerlink" title="4.require"></a>4.require</h4><ul>
<li>require 标识符需要满足此处的条件函数才会继续往下进行</li>
<li>语法如下：</li>
</ul>
<blockquote>
<p>require(bool expression,”false massage”);</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">//SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">contract FundMe &#123;</span><br><span class="line">     uint public value;</span><br><span class="line">    // fund函数，所有人都可以向该合约发送代币</span><br><span class="line">    function fund() public payable&#123;</span><br><span class="line">        //至少1ether才能发送</span><br><span class="line">        value =msg.value;</span><br><span class="line">        require(msg.value&gt;1e18,&quot;Didn&#x27;t send enough one ether&quot;);//1e18==1*10**18==1 ether</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="5-revert"><a href="#5-revert" class="headerlink" title="5. revert"></a>5. revert</h4><ul>
<li><p>require不满足条件时，撤销之前的操作，退回剩余的Gas</p>
</li>
<li><p>虽然不满足require，但是之前的操作是需要耗费gas的</p>
</li>
</ul>
<h3 id="十二、ChainLink-amp-Oracles"><a href="#十二、ChainLink-amp-Oracles" class="headerlink" title="十二、ChainLink &amp; Oracles"></a>十二、ChainLink &amp; Oracles</h3><p>链接地址：<a target="_blank" rel="noopener" href="https://data.chain.link/">chainlink</a></p>
<p>chainlink文档地址：<a target="_blank" rel="noopener" href="https://docs.chain.link/">Docs</a></p>
<p><img src="/notes/....%5Cimages%5Csolidity%5CuTools_1668394833158.png"></p>
<h4 id="chainlink"><a href="#chainlink" class="headerlink" title="chainlink"></a>chainlink</h4><ul>
<li>一个去中心化的预言网络用于带来外部数据和计算到智能合约中</li>
<li>结合链上和链下的混合智能合约</li>
<li>chainlink是一个模块化的去中心化预言机网络</li>
<li>可以定制提供数据和外部计算</li>
</ul>
<h5 id="1-chainlink-Data-Feeds"><a href="#1-chainlink-Data-Feeds" class="headerlink" title="1. chainlink Data Feeds"></a>1. chainlink Data Feeds</h5><ol>
<li>chainlink的节点从不同的交易所和数据提供者获取数据， </li>
<li>chainlink节点计算出实时的价格，然后提交单个事务到参考合约中。</li>
<li>其他合约就可以调用使用该数据</li>
</ol>
<p><img src="D:\github\Blog\images\solidity\uTools_1668396478849.png"></p>
<h5 id="2-chainlink-vrf"><a href="#2-chainlink-vrf" class="headerlink" title="2. chainlink vrf"></a>2. chainlink vrf</h5><h5 id="3-chainlinkKeepers"><a href="#3-chainlinkKeepers" class="headerlink" title="3. chainlinkKeepers"></a>3. chainlinkKeepers</h5><h5 id="4-connect-to-any-API"><a href="#4-connect-to-any-API" class="headerlink" title="4.connect to any API"></a>4.connect to any API</h5><h4 id="2-实现fundme的智能合约交互"><a href="#2-实现fundme的智能合约交互" class="headerlink" title="2. 实现fundme的智能合约交互"></a>2. 实现fundme的智能合约交互</h4><p>从chainlink的智能合约中获取链下数据（以太坊的价格）。需要智能合约的地址&amp;&amp;合约的ABI字节码</p>
<ol>
<li>去chainlink的<a target="_blank" rel="noopener" href="https://docs.chain.link/docs/data-feeds/price-feeds/addresses/#Goerli%20Testnet">DOCS</a>文档找到智能合约地址。</li>
</ol>
<p><img src="D:\github\Blog\images\solidity\uTools_1668484321400.png"></p>
<ol start="2">
<li>去<a target="_blank" rel="noopener" href="https://github.com/smartcontractkit/chainlink/blob/master/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol">GitHub</a>找到该合约的源码。</li>
<li>使用npm&amp;GitHub导入contract源码。</li>
<li>创建合约对象，调用源码中的函数。</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">//SPDX-License-Identifier: MIT</span><br><span class="line">pragma solidity 0.8.7;</span><br><span class="line">import &quot;@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol&quot;;</span><br><span class="line"></span><br><span class="line">contract FundMe &#123;</span><br><span class="line">     uint public value;</span><br><span class="line">    // fund函数，所有人都可以向该合约发送代币</span><br><span class="line">    function fund() public payable&#123;</span><br><span class="line">        //至少1ether才能发送</span><br><span class="line">        value =msg.value;</span><br><span class="line">        require(msg.value&gt;1e18,&quot;Didn&#x27;t send enough one ether&quot;);//1e18==1*10**18==1 ether</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    function getPrice() public view returns (int)&#123;</span><br><span class="line">        //ABI</span><br><span class="line">        //contract address  0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e</span><br><span class="line">        AggregatorV3Interface aggregatorV3Interface = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);</span><br><span class="line">         (</span><br><span class="line">            /*uint80 roundID*/,</span><br><span class="line">            int price,</span><br><span class="line">            /*uint startedAt*/,</span><br><span class="line">            /*uint timeStamp*/,</span><br><span class="line">            /*uint80 answeredInRound*/</span><br><span class="line">        ) = aggregatorV3Interface.latestRoundData();</span><br><span class="line">        return price ;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="十三、import合约"><a href="#十三、import合约" class="headerlink" title="十三、import合约"></a>十三、import合约</h3><h5 id="1-通过源文件相对位置导入，在solidity中，当你有多个文件并且想把一个文件导入文件时可以用import。"><a href="#1-通过源文件相对位置导入，在solidity中，当你有多个文件并且想把一个文件导入文件时可以用import。" class="headerlink" title="1.通过源文件相对位置导入，在solidity中，当你有多个文件并且想把一个文件导入文件时可以用import。"></a>1.通过源文件相对位置导入，在solidity中，当你有多个文件并且想把一个文件导入文件时可以用import。</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">import&#x27;./someothercontract.sol&#x27;;</span><br><span class="line">//  ./表示同一目录</span><br><span class="line">contract newContract is SomeOtherContract&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="2-通过源文件网址导入网上的合约。"><a href="#2-通过源文件网址导入网上的合约。" class="headerlink" title="2.通过源文件网址导入网上的合约。"></a>2.通过源文件网址导入网上的合约。</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// 通过网址引用</span><br><span class="line">import &#x27;https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol&#x27;;</span><br></pre></td></tr></table></figure>

<h5 id="3-通过npm的目录导入"><a href="#3-通过npm的目录导入" class="headerlink" title="3.通过npm的目录导入"></a>3.通过npm的目录导入</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import &#x27;@openzeppelin/contracts/access/Ownable.sol&#x27;;</span><br></pre></td></tr></table></figure>

<h5 id="4-通过全局符号导入特定合约"><a href="#4-通过全局符号导入特定合约" class="headerlink" title="4.通过全局符号导入特定合约"></a>4.通过全局符号导入特定合约</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import &#123;NewContract&#125; from &#x27;./SomeOtherContract.sol&#x27;;</span><br></pre></td></tr></table></figure>

<h3 id="十四、Library"><a href="#十四、Library" class="headerlink" title="十四、Library"></a>十四、Library</h3><h3 id="十五、modifier"><a href="#十五、modifier" class="headerlink" title="十五、modifier"></a>十五、modifier</h3>
      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://imynniu.gitee.io/notes/2022/11/27/solidity%E5%9F%BA%E7%A1%80/" title="solidity基础部分" target="_blank" rel="external">https://imynniu.gitee.io/notes/2022/11/27/solidity基础/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://gitee.com/imynniu" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/notes/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://gitee.com/imynniu" target="_blank"><span class="text-dark">Imynniu</span><small class="ml-1x">Web3 enthusiast &amp; Student</small></a></h3>
        <div>Web3游民。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/notes/2022/11/30/ether.js/" title="ether.js"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/notes/2022/11/27/solidity%E8%BF%9B%E9%98%B6/" title="solidity进阶部分"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/imynniu" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/notes/js/plugin.min.js"></script>


<script src="/notes/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/notes/',
        CONTENT_URL: '/notes/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/notes/js/insight.js"></script>






   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





   


  <script src="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.3.5/dist/jquery.fancybox.min.js"></script>
  <script>
  //利用 FancyBox 实现点击图片放大
  $(document).ready(function() {
    $('article img').not('[hidden]').not('.panel-body img').each(function() {
      var $image = $(this);
      var imageCaption = $image.attr('alt');
      var $imageWrapLink = $image.parent('a');
      if ($imageWrapLink.length < 1) {
        var src = this.getAttribute('src');
        var idx = src.lastIndexOf('?');
        if (idx != -1) {
          src = src.substring(0, idx);
        }
        $imageWrapLink = $image.wrap('<a href="' + src + '"></a>').parent('a');
      }
      $imageWrapLink.attr('data-fancybox', 'images');
      if (imageCaption) {
        $imageWrapLink.attr('data-caption', imageCaption);
      }
    });
    $().fancybox({
      selector: '[data-fancybox="images"]',
      hash: false,
      loop: false,
    });
  });
  </script>





</body>
</html>